package simple

import (
	"bytes"
	"strconv"
)

type ListNode struct {
	Val  int
	Next *ListNode
}

func NewListNode(vals ...int) *ListNode {
	head := &ListNode{vals[0], nil}

	node := head
	for i := 1; i < len(vals); i++ {
		node.Next = &ListNode{vals[i], nil}
		node = node.Next
	}
	return head
}

func NewCycleList(vals ...int) *ListNode {
	head := &ListNode{vals[0], nil}

	node := head
	for i := 1; i < len(vals); i++ {
		node.Next = &ListNode{vals[i], nil}
		node = node.Next
	}
	node.Next = head
	return head
}

func (node *ListNode) String() string {
	if node == nil {
		return ""
	}

	cur, buffer := node, bytes.NewBufferString("")
	for cur != nil {
		buffer.WriteString(strconv.Itoa(cur.Val))
		buffer.WriteString("->")
		cur = cur.Next
	}
	buffer.WriteString("nil")
	return buffer.String()
}
